home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume15 / moontool < prev    next >
Encoding:
Internet Message Format  |  1988-06-02  |  27.3 KB

  1. Subject:  v15i033:  The moon on a Sun
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4.  
  5. Submitted-by: John Walker <decwrl!acad!acad!kelvin>
  6. Posting-number: Volume 15, Issue 33
  7. Archive-name: moontool
  8.  
  9. [  There's no manual page.  --r$ ]
  10.  
  11. This is a little program for a Sun which displays an icon which approximates
  12. the current phase of the moon and, when opened, displays information on
  13. the Moon and Sun.  While the display code is highly Sun-specific, the
  14. calculation code is applicable to any system and can serve as the basis
  15. for ports to other window systems.
  16.  
  17. #! /bin/sh
  18. # This is a shell archive, meaning:
  19. # 1. Remove everything above the #! /bin/sh line.
  20. # 2. Save the resulting text in a file.
  21. # 3. Execute the file with /bin/sh (not csh) to create the files:
  22. #    README
  23. #    moontool.c
  24. #    makefile
  25. export PATH; PATH=/bin:$PATH
  26. echo shar: extracting "'README'" '(1058 characters)'
  27. if test -f 'README'
  28. then
  29.        echo shar: will not over-write existing file "'README'"
  30. else
  31. cat << \SHAR_EOF > 'README'
  32.  
  33.                      A Moon for the Sun, Release 2.0
  34.  
  35. What good is a Sun without a Moon?  This program implements a Suntool which
  36. displays the current phase of the Moon when iconic, and lots of information
  37. about the Sun and Moon when open.  This has been tested on a Sun 3/260
  38. under Sun 3.4, and a Sun 4/260, also under release 3.4.
  39.  
  40. The program was written by:
  41.  
  42.        John Walker
  43.        Autodesk, Inc.
  44.        2320 Marinship Way
  45.        Sausalito, CA  94965
  46.        (415) 332-2344 Ext. 829
  47.  
  48.        Usenet: {sun!well}!acad!kelvin
  49.  
  50. This program is in the public domain: "Do what thou wilt shall be the
  51. whole of the law".  I'd appreciate receiving any bug fixes and/or
  52. enhancements, which I'll incorporate in future versions of the
  53. program.  Please leave the original attribution information intact so
  54. that credit and blame may be properly apportioned.
  55.  
  56. Sources for the algorithms used by the program are cited in the comment
  57. at the beginning of moontool.c.
  58.  
  59. The tool is built with the command:
  60.  
  61. cc -O moontool.c -o moontool -lm -lsuntool -lsunwindow -lpixrect
  62. SHAR_EOF
  63. fi # end of overwriting check
  64. echo shar: extracting "'moontool.c'" '(25228 characters)'
  65. if test -f 'moontool.c'
  66. then
  67.        echo shar: will not over-write existing file "'moontool.c'"
  68. else
  69. cat << \SHAR_EOF > 'moontool.c'
  70. /*
  71.  
  72.     A Moon for the Sun
  73.  
  74.     Release 2.0
  75.  
  76.     Designed and implemented by John Walker in December 1987,
  77.     revised and updated in February of 1988.
  78.  
  79.     Make with:
  80.  
  81.     cc -O moontool.c -o moontool -lm -lsuntool -lsunwindow -lpixrect
  82.  
  83.     Adding  appropriate  floating  point  options  to your hardware.  This
  84.     program is a SunView tool which displays, as the  icon  for  a  closed
  85.     window,  the  current phase of the Moon.  A subtitle in the icon gives
  86.     the age of the Moon in days  and  hours.   If  called  with  the  "-t"
  87.     switch,  it  rapidly  increments  forward  through time to display the
  88.     cycle of phases.
  89.  
  90.     If you open the window, additional information is displayed  regarding
  91.     the  Moon.     The  information  is  generally  accurate  to    within ten
  92.     minutes.
  93.  
  94.     The algorithms used in this program to calculate the positions Sun and
  95.     Moon as seen from the Earth are given in the book "Practical Astronomy
  96.     With  Your  Calculator"  by  Peter  Duffett-Smith,   Second   Edition,
  97.     Cambridge University Press, 1981.  Ignore the word "Calculator" in the
  98.     title;  this  is  an  essential  reference  if  you're  interested  in
  99.     developing    software  which  calculates  planetary    positions, orbits,
  100.     eclipses, and  the  like.   If  you're  interested  in  pursuing  such
  101.     programming, you should also obtain:
  102.  
  103.     "Astronomical  Formulae for Calculators" by Jean Meeus, Third Edition,
  104.     Willmann-Bell, 1985.  A must-have.
  105.  
  106.     "Planetary  Programs  and  Tables  from  -4000  to  +2800"  by  Pierre
  107.     Bretagnon  and Jean-Louis Simon, Willmann-Bell, 1986.  If you want the
  108.     utmost  (outside  of  JPL)  accuracy  for  the  planets,  it's   here.
  109.  
  110.     "Celestial BASIC" by Eric Burgess, Revised Edition, Sybex, 1985.  Very
  111.     cookbook oriented, and many of the algorithms are hard to dig  out    of
  112.     the turgid BASIC code, but you'll probably want it anyway.
  113.  
  114.     Many of these references can be obtained from Willmann-Bell, P.O.  Box
  115.     35025,  Richmond,  VA 23235, USA.  Phone: (804) 320-7016.  In addition
  116.     to their own publications, they stock most of the standard    references
  117.     for mathematical and positional astronomy.
  118.  
  119.     This program was written by:
  120.  
  121.        John Walker
  122.        Autodesk, Inc.
  123.        2320 Marinship Way
  124.        Sausalito, CA  94965
  125.        (415) 332-2344 Ext. 829
  126.  
  127.        Usenet: {sun!well}!acad!kelvin
  128.  
  129.     This  program is in the public domain: "Do what thou wilt shall be the
  130.     whole of the law".  I'd appreciate  receiving  any  bug  fixes  and/or
  131.     enhancements,  which  I'll  incorporate  in  future  versions  of  the
  132.     program.  Please leave the original attribution information intact    so
  133.     that credit and blame may be properly apportioned.
  134.  
  135. */
  136.  
  137. /*  Astronomical constants  */
  138.  
  139. #define epoch        2444238.5       /* 1980 January 0.0 */
  140.  
  141. /*  Constants defining the Sun's apparent orbit  */
  142.  
  143. #define elonge        278.833540       /* Ecliptic longitude of the Sun
  144.                       at epoch 1980.0 */
  145. #define elongp        282.596403       /* Ecliptic longitude of the Sun at
  146.                       perigee */
  147. #define eccent      0.016718       /* Eccentricity of Earth's orbit */
  148. #define sunsmax     1.495985e8     /* Semi-major axis of Earth's orbit, km */
  149. #define sunangsiz   0.533128       /* Sun's angular size, degrees, at
  150.                       semi-major axis distance */
  151.  
  152. /*  Elements of the Moon's orbit, epoch 1980.0  */
  153.  
  154. #define mmlong      64.975464      /* Moon's mean lonigitude at the epoch */
  155. #define mmlongp     349.383063       /* Mean longitude of the perigee at the
  156.                       epoch */
  157. #define mlnode        151.950429       /* Mean longitude of the node at the
  158.                       epoch */
  159. #define minc        5.145396       /* Inclination of the Moon's orbit */
  160. #define mecc        0.054900       /* Eccentricity of the Moon's orbit */
  161. #define mangsiz     0.5181         /* Moon's angular size at distance a
  162.                       from Earth */
  163. #define msmax       384401.0       /* Semi-major axis of Moon's orbit in km */
  164. #define mparallax   0.9507       /* Parallax at distance a from Earth */
  165. #define synmonth    29.53058868    /* Synodic month (new Moon to new Moon) */
  166. #define lunatbase   2423436.0      /* Base date for E. W. Brown's numbered
  167.                       series of lunations (1923 January 16) */
  168.  
  169. /*  Properties of the Earth  */
  170.  
  171. #define earthrad    6378.16       /* Radius of Earth in kilometres */
  172.  
  173. #include <stdio.h>
  174. #include <math.h>
  175.  
  176. #include <suntool/sunview.h>
  177. #include <suntool/canvas.h>
  178.  
  179. /* Icon definition.  This is just a black field with rounded corners
  180.    that blend into the root desktop pattern. The image of the moon and
  181.    the text are added by the program later. */
  182.  
  183. static short moon_img[64][4] = {
  184. /* Format_version=1, Width=64, Height=64, Depth=1, Valid_bits_per_item=16 */
  185.     0x8FFF,0xFFFF,0xFFFF,0xFFE8,0x9FFF,0xFFFF,0xFFFF,0xFFF8,
  186.     0x3FFF,0xFFFF,0xFFFF,0xFFFE,0x7FFF,0xFFFF,0xFFFF,0xFFFE,
  187.     0xFFFF,0xFFFF,0xFFFF,0xFFFE,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  188.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  189.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  190.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  191.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  192.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  193.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  194.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  195.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  196.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  197.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  198.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  199.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  200.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  201.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  202.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  203.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  204.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  205.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  206.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  207.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  208.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  209.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  210.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  211.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  212.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  213.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
  214.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x7FFF,0xFFFF,0xFFFF,0xFFFE,
  215.     0xFFFF,0xFFFF,0xFFFF,0xFFFE,0xBFFF,0xFFFF,0xFFFF,0xFFFC,
  216.     0x3FFF,0xFFFF,0xFFFF,0xFFFA,0x27FF,0xFFFF,0xFFFF,0xFFE2
  217. };
  218. DEFINE_ICON_FROM_IMAGE(moon_icon, moon_img);
  219. mpr_static(icon_mpr, 64, 64, 1, moon_img);
  220.  
  221. static Frame frame;
  222. static Pixfont *pfont;
  223. static Canvas canvas;
  224. static Pixwin *cpw;
  225. static int charhgt, charwid;
  226.  
  227. #define PI 3.14159265358979323846  /* Assume not near black hole nor in
  228.                       Tennessee */
  229.  
  230. /*  Handy mathematical functions  */
  231.  
  232. #define sgn(x) (((x) < 0) ? -1 : ((x) > 0 ? 1 : 0))      /* Extract sign */
  233. #define abs(x) ((x) < 0 ? (-(x)) : (x))           /* Absolute val */
  234. #define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0)))  /* Fix angle      */
  235. #define torad(d) ((d) * (PI / 180.0))              /* Deg->Rad      */
  236. #define todeg(d) ((d) * (180.0 / PI))              /* Rad->Deg      */
  237. #define dsin(x) (sin(torad((x))))              /* Sin from deg */
  238. #define dcos(x) (cos(torad((x))))              /* Cos from deg */
  239.  
  240. static int testmode = FALSE;       /* Rapid warp through time for debugging */
  241.  
  242. /*  Forward functions  */
  243.  
  244. double jtime(), phase();
  245. void phasehunt();
  246. Notify_value ringgg();
  247. void drawmoon(), jyear(), jhms();
  248.  
  249. /*  Main program  */
  250.  
  251. main(argc, argv)
  252. int argc;
  253. char *argv[];
  254. {
  255.     int i;
  256.     struct itimerval kickme;
  257.     struct pixfont *font;
  258.  
  259.     for (i = 1; i < argc; i++) {
  260.            if (*argv[i] == '-' && argv[i][1] == 't')
  261.           testmode = TRUE;
  262.     }
  263.         pfont = pf_open("/usr/lib/fonts/fixedwidthfonts/screen.r.7");
  264.     frame = window_create(NULL, FRAME,
  265.          FRAME_LABEL,
  266.         "A Moon for the Sun          by John Walker, Autodesk, Inc.   v2.0",
  267.          FRAME_ICON, &moon_icon,
  268.          FRAME_ARGS, argc, argv,
  269.          FRAME_CLOSED, 1,
  270.                  WIN_ERROR_MSG, "Can't create window.",
  271.           0);
  272.     canvas = window_create(frame, CANVAS, CANVAS_RETAINED, TRUE, 0);
  273.     cpw = canvas_pixwin(canvas);
  274.     font = (struct pixfont *) window_get(canvas, WIN_FONT);
  275.     charwid = font->pf_defaultsize.x;
  276.     charhgt = font->pf_defaultsize.y;
  277.     window_set(frame, WIN_WIDTH, charwid * 70,
  278.               WIN_HEIGHT, charhgt * 19, 0);
  279.     ringgg((Notify_client) NULL, 0);
  280.     kickme.it_interval.tv_usec = kickme.it_value.tv_usec =
  281.        testmode ? 125000 : 0;
  282.     kickme.it_interval.tv_sec = kickme.it_value.tv_sec = 
  283.        testmode ? 0 : 1;
  284.     notify_set_itimer_func(frame, ringgg, ITIMER_REAL,
  285.        &kickme, NULL);
  286.     window_main_loop(frame);
  287. }
  288.  
  289. /*  DRAWMOON  --  Construct icon for moon, given phase of moon.  */
  290.  
  291. static void drawmoon(ph)
  292. double ph;
  293. {
  294.     int i, j, lx, rx;
  295.     int lb[4];
  296.     double cp, xscale;
  297.  
  298.     xscale = cos(2 * PI * ph);
  299.     for (i = 0; i < 24; i++) {
  300.        lb[0] = lb[1] = lb[2] = lb[3] = 0xFFFF;
  301.        cp = 24.0 * cos(asin(i / 24.0));
  302.        if (ph < 0.5) {
  303.           rx = 32 + cp;
  304.           lx = 32 + xscale * cp;
  305.        } else {
  306.           lx = 33 - cp;
  307.           rx = 33 - xscale * cp;
  308.        }
  309.        for (j = lx; j <= rx; j++) {
  310.           lb[j >> 4] &= (0x8000 >> (j & 0xF)) ^ 0xFFFF;
  311.        }
  312.        for (j = 0; j < 4; j++)
  313.           moon_img[28 + i][j] = moon_img[28 - i][j] = lb[j];
  314.     }
  315. }
  316.  
  317. /*  RINGGG  --    Update status on interval timer ticks and redraw
  318.         window if needed.  */
  319.  
  320. static Notify_value ringgg(client, itimer_type)
  321. Notify_client client;
  322. int itimer_type;
  323. {
  324.     int lunation, wclosed;
  325.     long t;
  326.     double jd, p, aom, cphase, cdist, cangdia, csund, csuang, lptime;
  327.     double phasar[5];
  328.     static double nptime = 0.0; /* Next new moon time */
  329.     static int updyet = 0;        /* Update interval when window closed */
  330.     static int firstime = TRUE; /* Calculate text page first time */
  331.     struct pr_prpos tloc;
  332.     char amsg[12], tbuf[80];
  333.     static double faketime = 0.0;
  334.     static short moonilast[64][4] = {0};
  335.     int yy, mm, dd, hh, mmm, ss;
  336.     struct tm *gm;
  337.         static char *moname[] = {"January", "February", "March",
  338.            "April", "May", "June", "July", "August", "September",
  339.            "October", "November", "December"};
  340.  
  341. #define CUPDINT  120           /* Update the icon every CUPDINT seconds
  342.                       when the window is iconic */
  343.  
  344.     /* If the window is closed, only update the icon every
  345.        two minutes */
  346.  
  347.     wclosed = (int) window_get(frame, FRAME_CLOSED);
  348.     if (wclosed && (--updyet > 0) && !testmode)
  349.        return;
  350.  
  351.     updyet = CUPDINT;
  352.     (void) time(&t);
  353.     jd = jtime((gm = gmtime(&t)));
  354.     if (testmode) {
  355.        if (faketime == 0.0)
  356.           faketime = jd + 1;
  357.        else
  358.           faketime += 1.0 / 24;
  359.        jd = faketime;
  360.     }
  361.     p = phase(jd, &cphase, &aom, &cdist, &cangdia, &csund, &csuang);
  362.     drawmoon(p);
  363.         sprintf(amsg, " %dd %dh  ",
  364.        (int) aom, ((int) (24 * (aom - floor(aom)))));
  365.     tloc.pr = (Pixrect *) icon_get(&moon_icon, ICON_IMAGE);
  366.     tloc.pos.x = 2;
  367.     tloc.pos.y = 62;
  368.     pf_text(tloc, PIX_NOT(PIX_SRC), pfont, amsg);
  369.  
  370.     /* Only update icon if it changed (this eliminates gratuitous
  371.        flashing of the icon on-screen). */
  372.  
  373.     if (bcmp(moonilast, moon_img, sizeof moon_img) != 0) {
  374.        bcopy(moon_img, moonilast, sizeof moon_img);
  375.        window_set(frame, FRAME_ICON, &moon_icon, 0);
  376.     }
  377.  
  378.         /* If we're iconic, there's nothing more to do. */
  379.  
  380.     if (wclosed && !firstime)
  381.        return;
  382.  
  383.     /* Update textual information for open window */
  384.  
  385. #define prt(x) pw_text(cpw, charwid, charhgt * (x), PIX_SRC, NULL, tbuf)
  386. #define prtxy(x,y) pw_text(cpw,charwid*(y+1),charhgt*(x),PIX_SRC,NULL,tbuf)
  387.  
  388.     firstime = FALSE;
  389.         sprintf(tbuf, "Julian date:     %.5f", jd + 0.5);
  390.     prt(1);
  391.     if (testmode) {
  392.        jyear(jd, &yy, &mm, &dd);
  393.        jhms(jd, &hh, &mmm, &ss);
  394.        sprintf(tbuf,
  395.               "Universal time:  %02d:%02d:%02d %d %s %d              ",
  396.           hh, mmm, ss, dd, moname[mm - 1], yy);
  397.     } else {
  398.        sprintf(tbuf,
  399.               "Universal time:  %02d:%02d:%02d %d %s %d              ",
  400.           gm->tm_hour, gm->tm_min, gm->tm_sec,
  401.           gm->tm_mday, moname[gm->tm_mon], gm->tm_year + 1900);
  402.     }
  403.     prt(2);
  404.     gm = localtime(&t);
  405.         sprintf(tbuf, "Local time:      %02d:%02d:%02d %d %s %d              ",
  406.        gm->tm_hour, gm->tm_min, gm->tm_sec,
  407.        gm->tm_mday, moname[gm->tm_mon], gm->tm_year + 1900);
  408.     if (!testmode) {       /* Ignore local time in test mode */
  409.        prt(3);
  410.     }
  411.         sprintf(tbuf, "Moon phase:      %d%%   0%% = New, 100%% = Full  ",
  412.        (int) (cphase * 100));
  413.     prt(5);
  414.  
  415.     /* Information about the Moon */
  416.  
  417. #define EPL(x) (x), (x) == 1 ? "" : "s"
  418.     sprintf(tbuf,
  419.            "Age of moon:     %d day%s, %d hour%s, %d minute%s.       ",
  420.        EPL((int) aom), EPL(((int) (24 * (aom - floor(aom))))),
  421.        EPL(((int) (1440 * (aom - floor(aom)))) % 60));
  422.     prt(6);
  423.     sprintf(tbuf,
  424.            "Moon's distance: %ld kilometres, %.1f Earth radii.  ",
  425.        (long) cdist, cdist / earthrad);
  426.     prt(7);
  427.     sprintf(tbuf,
  428.            "Moon subtends:   %.4f degrees.       ", cangdia);
  429.     prt(8);
  430.     /* Draw the moon icon in the text window */
  431.     pw_rop(cpw, 60 * charwid, 4 * charhgt, 64, 64, PIX_SRC,
  432.        &icon_mpr, 0, 0);
  433.  
  434.     /* Edit information about the Sun */
  435.  
  436.     sprintf(tbuf,
  437.            "Sun's distance:  %.0f kilometres, %.3f astronomical units.       ",
  438.         csund, csund / sunsmax);
  439.     prt(10);
  440.     sprintf(tbuf,
  441.            "Sun subtends:    %.4f degrees.       ", csuang);
  442.     prt(11);
  443.  
  444.     /* Calculate times of phases of this lunation.    This is sufficiently
  445.        time-consuming that we only do it once a month. */
  446.  
  447.     if (jd > nptime) {
  448. #define APOS(x) (x + 13)
  449.        phasehunt(jd, phasar);
  450.        lptime = phasar[0];
  451.        lunation = floor(((lptime + 7) - lunatbase) / synmonth) + 1;
  452.        jyear(lptime, &yy, &mm, &dd);
  453.        jhms(lptime, &hh, &mmm, &ss);
  454.        sprintf(tbuf,
  455.               "Last new moon:   %02d:%02d UTC %d %s %d            ",
  456.           hh, mmm, dd, moname[mm - 1], yy);
  457.        prt(APOS(0));
  458.            sprintf(tbuf, "Lunation %d    ", lunation);
  459.        prtxy(APOS(0), 47);                  
  460.  
  461.        lptime = phasar[1];
  462.        jyear(lptime, &yy, &mm, &dd);
  463.        jhms(lptime, &hh, &mmm, &ss);
  464.        sprintf(tbuf,
  465.               "First quarter:   %02d:%02d UTC %d %s %d              ",
  466.           hh, mmm, dd, moname[mm - 1], yy);
  467.        prt(APOS(1));
  468.  
  469.        lptime = phasar[2];
  470.        jyear(lptime, &yy, &mm, &dd);
  471.        jhms(lptime, &hh, &mmm, &ss);
  472.        sprintf(tbuf,
  473.               "Full moon:       %02d:%02d UTC %d %s %d              ",
  474.           hh, mmm, dd, moname[mm - 1], yy);
  475.        prt(APOS(2));
  476.  
  477.        lptime = phasar[3];
  478.        jyear(lptime, &yy, &mm, &dd);
  479.        jhms(lptime, &hh, &mmm, &ss);
  480.        sprintf(tbuf,
  481.               "Last quarter:    %02d:%02d UTC %d %s %d              ",
  482.           hh, mmm, dd, moname[mm - 1], yy);
  483.        prt(APOS(3));
  484.  
  485.        nptime = phasar[4];
  486.        jyear(nptime, &yy, &mm, &dd);
  487.        jhms(nptime, &hh, &mmm, &ss);
  488.        sprintf(tbuf,
  489.               "Next new moon:   %02d:%02d UTC %d %s %d              ",
  490.           hh, mmm, dd, moname[mm - 1], yy);
  491.        prt(APOS(4));
  492.            sprintf(tbuf, "Lunation %d    ", lunation + 1);
  493.        prtxy(APOS(4), 47);                  
  494.     }
  495. #undef APOS
  496. }
  497.  
  498. /*  JDATE  --  Convert internal GMT date and time to Julian day
  499.            and fraction.  */
  500.  
  501. static long jdate(t)
  502. struct tm *t;
  503. {
  504.     long c, m, y;
  505.  
  506.     y = t->tm_year + 1900;
  507.     m = t->tm_mon + 1;
  508.     if (m > 2)
  509.        m = m - 3;
  510.     else {
  511.        m = m + 9;
  512.        y--;
  513.     }
  514.     c = y / 100L;           /* Compute century */
  515.     y -= 100L * c;
  516.     return t->tm_mday + (c * 146097L) / 4 + (y * 1461L) / 4 +
  517.         (m * 153L + 2) / 5 + 1721119L;
  518. }
  519.  
  520. /* JTIME --    Convert internal GMT date and time to astronomical Julian
  521.            time (i.e. Julian date plus day fraction, expressed as
  522.            a double).  */
  523.  
  524. static double jtime(t)
  525. struct tm *t;
  526. {
  527.     return (jdate(t) - 0.5) + 
  528.        (t->tm_sec + 60 * (t->tm_min + 60 * t->tm_hour)) / 86400.0;
  529. }
  530.  
  531. /*  JYEAR  --  Convert Julian date to year, month, day, which are
  532.            returned via integer pointers to integers.  */
  533.  
  534. static void jyear(td, yy, mm, dd)
  535. double td;
  536. int *yy, *mm, *dd;
  537. {
  538.     double j, d, y, m;
  539.  
  540.     td += 0.5;           /* Astronomical to civil */
  541.     j = floor(td);
  542.     j = j - 1721119.0;
  543.     y = floor(((4 * j) - 1) / 146097.0);
  544.     j = (j * 4.0) - (1.0 + (146097.0 * y));
  545.     d = floor(j / 4.0);
  546.     j = floor(((4.0 * d) + 3.0) / 1461.0);
  547.     d = ((4.0 * d) + 3.0) - (1461.0 * j);
  548.     d = floor((d + 4.0) / 4.0);
  549.     m = floor(((5.0 * d) - 3) / 153.0);
  550.     d = (5.0 * d) - (3.0 + (153.0 * m));
  551.     d = floor((d + 5.0) / 5.0);
  552.     y = (100.0 * y) + j;
  553.     if (m < 10.0)
  554.        m = m + 3;
  555.     else {
  556.        m = m - 9;
  557.        y = y + 1;
  558.     }
  559.     *yy = y;
  560.     *mm = m;
  561.     *dd = d;
  562. }
  563.  
  564. /*  JHMS  --  Convert Julian time to hour, minutes, and seconds.  */
  565.  
  566. static void jhms(j, h, m, s)
  567. double j;
  568. int *h, *m, *s;
  569. {
  570.     long ij;
  571.  
  572.     j += 0.5;           /* Astronomical to civil */
  573.     ij = (j - floor(j)) * 86400.0;
  574.     *h = ij / 3600L;
  575.     *m = (ij / 60L) % 60L;
  576.     *s = ij % 60L;
  577. }
  578.  
  579. /*  MEANPHASE  --  Calculates mean phase of the Moon for a given
  580.            base date and desired phase:
  581.                0.0   New Moon
  582.                0.25  First quarter
  583.                0.5   Full moon
  584.                0.75  Last quarter
  585.             Beware!!!  This routine returns meaningless
  586.                     results for any other phase arguments.  Don't
  587.             attempt to generalise it without understanding
  588.             that the motion of the moon is far more complicated
  589.             that this calculation reveals. */
  590.  
  591. static double meanphase(sdate, phase, usek)
  592. double sdate, phase;
  593. double *usek;
  594. {
  595.     int yy, mm, dd;
  596.     double k, t, t2, t3, nt1;
  597.  
  598.     jyear(sdate, &yy, &mm, &dd);
  599.  
  600.     k = (yy + ((mm - 1) * (1.0 / 12.0)) - 1900) * 12.3685;
  601.  
  602.     /* Time in Julian centuries from 1900 January 0.5 */
  603.     t = (sdate - 2415020.0) / 36525;
  604.     t2 = t * t;           /* Square for frequent use */
  605.     t3 = t2 * t;           /* Cube for frequent use */
  606.  
  607.     *usek = k = floor(k) + phase;
  608.     nt1 = 2415020.75933 + synmonth * k
  609.           + 0.0001178 * t2
  610.           - 0.000000155 * t3
  611.           + 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
  612.  
  613.     return nt1;
  614. }
  615.  
  616. /*  TRUEPHASE  --  Given a K value used to determine the
  617.            mean phase of the new moon, and a phase
  618.            selector (0.0, 0.25, 0.5, 0.75), obtain
  619.            the true, corrected phase time.  */
  620.  
  621. static double truephase(k, phase)
  622. double k, phase;
  623. {
  624.     double t, t2, t3, pt, m, mprime, f;
  625.     int apcor = FALSE;
  626.  
  627.     k += phase;           /* Add phase to new moon time */
  628.     t = k / 1236.85;       /* Time in Julian centuries from
  629.                       1900 January 0.5 */
  630.     t2 = t * t;           /* Square for frequent use */
  631.     t3 = t2 * t;           /* Cube for frequent use */
  632.     pt = 2415020.75933       /* Mean time of phase */
  633.          + synmonth * k
  634.          + 0.0001178 * t2
  635.          - 0.000000155 * t3
  636.          + 0.00033 * dsin(166.56 + 132.87 * t - 0.009173 * t2);
  637.  
  638.         m = 359.2242               /* Sun's mean anomaly */
  639.         + 29.10535608 * k
  640.         - 0.0000333 * t2
  641.         - 0.00000347 * t3;
  642.         mprime = 306.0253          /* Moon's mean anomaly */
  643.         + 385.81691806 * k
  644.         + 0.0107306 * t2
  645.         + 0.00001236 * t3;
  646.         f = 21.2964                /* Moon's argument of latitude */
  647.         + 390.67050646 * k
  648.         - 0.0016528 * t2
  649.         - 0.00000239 * t3;
  650.     if ((phase < 0.01) || (abs(phase - 0.5) < 0.01)) {
  651.  
  652.        /* Corrections for New and Full Moon */
  653.  
  654.        pt +=     (0.1734 - 0.000393 * t) * dsin(m)
  655.             + 0.0021 * dsin(2 * m)
  656.             - 0.4068 * dsin(mprime)
  657.             + 0.0161 * dsin(2 * mprime)
  658.             - 0.0004 * dsin(3 * mprime)
  659.             + 0.0104 * dsin(2 * f)
  660.             - 0.0051 * dsin(m + mprime)
  661.             - 0.0074 * dsin(m - mprime)
  662.             + 0.0004 * dsin(2 * f + m)
  663.             - 0.0004 * dsin(2 * f - m)
  664.             - 0.0006 * dsin(2 * f + mprime)
  665.             + 0.0010 * dsin(2 * f - mprime)
  666.             + 0.0005 * dsin(m + 2 * mprime);
  667.        apcor = TRUE;
  668.     } else if ((abs(phase - 0.25) < 0.01 || (abs(phase - 0.75) < 0.01))) {
  669.        pt +=     (0.1721 - 0.0004 * t) * dsin(m)
  670.             + 0.0021 * dsin(2 * m)
  671.             - 0.6280 * dsin(mprime)
  672.             + 0.0089 * dsin(2 * mprime)
  673.             - 0.0004 * dsin(3 * mprime)
  674.             + 0.0079 * dsin(2 * f)
  675.             - 0.0119 * dsin(m + mprime)
  676.             - 0.0047 * dsin(m - mprime)
  677.             + 0.0003 * dsin(2 * f + m)
  678.             - 0.0004 * dsin(2 * f - m)
  679.             - 0.0006 * dsin(2 * f + mprime)
  680.             + 0.0021 * dsin(2 * f - mprime)
  681.             + 0.0003 * dsin(m + 2 * mprime)
  682.             + 0.0004 * dsin(m - 2 * mprime)
  683.             - 0.0003 * dsin(2 * m + mprime);
  684.        if (phase < 0.5)
  685.           /* First quarter correction */
  686.           pt += 0.0028 - 0.0004 * dcos(m) + 0.0003 * dcos(mprime);
  687.        else
  688.           /* Last quarter correction */
  689.           pt += -0.0028 + 0.0004 * dcos(m) - 0.0003 * dcos(mprime);
  690.        apcor = TRUE;
  691.     }
  692.     if (!apcor) {
  693.            fprintf(stderr, "TRUEPHASE called with invalid phase selector.\n");
  694.        abort();
  695.     }
  696.     return pt;
  697. }
  698.  
  699. /*  PHASEHUNT  --  Find time of phases of the moon which surround
  700.            the current date.  Five phases are found, starting
  701.            and ending with the new moons which bound the
  702.            current lunation.  */
  703.  
  704. static void phasehunt(sdate, phases)
  705. double sdate;
  706. double phases[5];
  707. {
  708.     double adate, k1, k2, nt1, nt2;
  709.  
  710.     adate = sdate - 45;
  711.     nt1 = meanphase(adate, 0.0, &k1);
  712.     while (TRUE) {
  713.        adate += synmonth;
  714.        nt2 = meanphase(adate, 0.0, &k2);
  715.        if (nt1 <= sdate && nt2 > sdate)
  716.           break;
  717.        nt1 = nt2;
  718.        k1 = k2;
  719.     }
  720.     phases[0] = truephase(k1, 0.0);
  721.     phases[1] = truephase(k1, 0.25);
  722.     phases[2] = truephase(k1, 0.5);
  723.     phases[3] = truephase(k1, 0.75);
  724.     phases[4] = truephase(k2, 0.0);
  725. }
  726.  
  727. /*  KEPLER  --    Solve the equation of Kepler.  */
  728.  
  729. static double kepler(m, ecc)
  730. double m, ecc;
  731. {
  732.     double e, delta;
  733. #define EPSILON 1E-6
  734.  
  735.     e = m = torad(m);
  736.     do {
  737.        delta = e - ecc * sin(e) - m;
  738.        e -= delta / (1 - ecc * cos(e));
  739.     } while (abs(delta) > EPSILON);
  740.     return e;
  741. }
  742.  
  743. /*  PHASE  --  Calculate phase of moon as a fraction:
  744.  
  745.     The argument is the time for which the phase is requested,
  746.     expressed as a Julian date and fraction.  Returns the terminator
  747.     phase angle as a percentage of a full circle (i.e., 0 to 1),
  748.     and stores into pointer arguments the illuminated fraction of
  749.         the Moon's disc, the Moon's age in days and fraction, the
  750.     distance of the Moon from the centre of the Earth, and the
  751.     angular diameter subtended by the Moon as seen by an observer
  752.     at the centre of the Earth.
  753.  
  754. */
  755.  
  756. static double phase(pdate, pphase, mage, dist, angdia, sudist, suangdia)
  757. double pdate;
  758. double *pphase;            /* Illuminated fraction */
  759. double *mage;               /* Age of moon in days */
  760. double *dist;               /* Distance in kilometres */
  761. double *angdia;            /* Angular diameter in degrees */
  762. double *sudist;            /* Distance to Sun */
  763. double *suangdia;                  /* Sun's angular diameter */
  764. {
  765.  
  766.     double Day, N, M, Ec, Lambdasun, ml, MM, MN, Ev, Ae, A3, MmP,
  767.            mEc, A4, lP, V, lPP, NP, y, x, Lambdamoon, BetaM,
  768.            MoonAge, MoonPhase,
  769.            MoonDist, MoonDFrac, MoonAng, MoonPar,
  770.            F, SunDist, SunAng;
  771.  
  772.         /* Calculation of the Sun's position */
  773.  
  774.     Day = pdate - epoch;        /* Date within epoch */
  775.     N = fixangle((360 / 365.2422) * Day); /* Mean anomaly of the Sun */
  776.     M = fixangle(N + elonge - elongp);    /* Convert from perigee
  777.                        co-ordinates to epoch 1980.0 */
  778.     Ec = kepler(M, eccent);     /* Solve equation of Kepler */
  779.     Ec = sqrt((1 + eccent) / (1 - eccent)) * tan(Ec / 2);
  780.     Ec = 2 * todeg(atan(Ec));   /* True anomaly */
  781.         Lambdasun = fixangle(Ec + elongp);  /* Sun's geocentric ecliptic
  782.                            longitude */
  783.     /* Orbital distance factor */
  784.     F = ((1 + eccent * cos(torad(Ec))) / (1 - eccent * eccent));
  785.     SunDist = sunsmax / F;        /* Distance to Sun in km */
  786.         SunAng = F * sunangsiz;     /* Sun's angular size in degrees */
  787.  
  788.  
  789.         /* Calculation of the Moon's position */
  790.  
  791.         /* Moon's mean longitude */
  792.     ml = fixangle(13.1763966 * Day + mmlong);
  793.  
  794.         /* Moon's mean anomaly */
  795.     MM = fixangle(ml - 0.1114041 * Day - mmlongp);
  796.  
  797.         /* Moon's ascending node mean longitude */
  798.     MN = fixangle(mlnode - 0.0529539 * Day);
  799.  
  800.     /* Evection */
  801.     Ev = 1.2739 * sin(torad(2 * (ml - Lambdasun) - MM));
  802.  
  803.     /* Annual equation */
  804.     Ae = 0.1858 * sin(torad(M));
  805.  
  806.     /* Correction term */
  807.     A3 = 0.37 * sin(torad(M));
  808.  
  809.     /* Corrected anomaly */
  810.     MmP = MM + Ev - Ae - A3;
  811.  
  812.     /* Correction for the equation of the centre */
  813.     mEc = 6.2886 * sin(torad(MmP));
  814.  
  815.     /* Another correction term */
  816.     A4 = 0.214 * sin(torad(2 * MmP));
  817.  
  818.     /* Corrected longitude */
  819.     lP = ml + Ev + mEc - Ae + A4;
  820.  
  821.     /* Variation */
  822.     V = 0.6583 * sin(torad(2 * (lP - Lambdasun)));
  823.  
  824.     /* True longitude */
  825.     lPP = lP + V;
  826.  
  827.     /* Corrected longitude of the node */
  828.     NP = MN - 0.16 * sin(torad(M));
  829.  
  830.     /* Y inclination coordinate */
  831.     y = sin(torad(lPP - NP)) * cos(torad(minc));
  832.  
  833.     /* X inclination coordinate */
  834.     x = cos(torad(lPP - NP));
  835.  
  836.     /* Ecliptic longitude */
  837.     Lambdamoon = todeg(atan2(y, x));
  838.     Lambdamoon += NP;
  839.  
  840.     /* Ecliptic latitude */
  841.     BetaM = todeg(asin(sin(torad(lPP - NP)) * sin(torad(minc))));
  842.  
  843.     /* Calculation of the phase of the Moon */
  844.  
  845.     /* Age of the Moon in degrees */
  846.     MoonAge = lPP - Lambdasun;
  847.  
  848.     /* Phase of the Moon */
  849.     MoonPhase = (1 - cos(torad(MoonAge))) / 2;
  850.  
  851.     /* Calculate distance of moon from the centre of the Earth */
  852.  
  853.     MoonDist = (msmax * (1 - mecc * mecc)) /
  854.        (1 + mecc * cos(torad(MmP + mEc)));
  855.  
  856.         /* Calculate Moon's angular diameter */
  857.  
  858.     MoonDFrac = MoonDist / msmax;
  859.     MoonAng = mangsiz / MoonDFrac;
  860.  
  861.         /* Calculate Moon's parallax */
  862.  
  863.     MoonPar = mparallax / MoonDFrac;
  864.  
  865.     *pphase = MoonPhase;
  866.     *mage = synmonth * (fixangle(MoonAge) / 360.0);
  867.     *dist = MoonDist;
  868.     *angdia = MoonAng;
  869.     *sudist = SunDist;
  870.     *suangdia = SunAng;
  871.     return fixangle(MoonAge) / 360.0;
  872. }
  873. SHAR_EOF
  874. fi # end of overwriting check
  875. echo shar: extracting "'makefile'" '(125 characters)'
  876. if test -f 'makefile'
  877. then
  878.        echo shar: will not over-write existing file "'makefile'"
  879. else
  880. cat << \SHAR_EOF > 'makefile'
  881.  
  882. #   Make instructions for moon tool
  883.  
  884. moontool: moontool.c
  885.     cc -O moontool.c -o moontool -lm -lsuntool -lsunwindow -lpixrect
  886. SHAR_EOF
  887. fi # end of overwriting check
  888. #    End of shell archive
  889. exit 0
  890.  
  891.  
  892.